﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CustomExtensionDialog.xaml.cs" company="Mar3ek">
//   This code is licensed under the Mozilla Public License 2.0.
//   You can find the full license text at the following address:
//   https://winkee.codeplex.com/license
// </copyright>
// <summary>
//   Defines the CustomExtensionDialog type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WinKee.Dialogs
{
    using System;
    using System.Linq;
    using System.Text.RegularExpressions;

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    public sealed partial class CustomExtensionDialog
    {
        private readonly Regex extensionValidator = new Regex("^[a-zA-Z0-9]+$", RegexOptions.IgnoreCase);

        public CustomExtensionDialog()
        {
            this.InitializeComponent();
            this.ExtensionTextBox.Focus(FocusState.Keyboard);
        }

        public string[] ExtensionStrings { get; private set; }

        private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            if (string.IsNullOrEmpty(this.ExtensionTextBox.Text))
            {
                return;
            }

            var split = this.ExtensionTextBox.Text.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (split.Any(s => !this.extensionValidator.IsMatch(s)))
            {
                this.WarningTextBlock.Visibility = Visibility.Visible;
                args.Cancel = true;
            }

            this.ExtensionStrings = split.Distinct().ToArray();
        }

        private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
        }
    }
}
